import joblib
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, classification_report, roc_auc_score, f1_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, MinMaxScaler, StandardScaler, OneHotEncoder
from imblearn.over_sampling import RandomOverSampler, SMOTE, ADASYN
from sklearn.linear_model import LogisticRegression

plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['font.size'] = 15

test_data = pd.read_csv('../data/test2.csv')
test_data.drop(['EmployeeNumber', 'Over18', 'StandardHours'], axis=1, inplace=True)
# Resources
X = test_data.drop('Attrition', axis=1)
# Target variable
y = test_data['Attrition']
# 打印特征和标签的形状
print(f"特征的形状{X.shape}")
# 打印样本的类别分布
print(f"样本的类别分布:\n{y.value_counts()}")

# 加载编码器
label_encoders = joblib.load('../encoder/lo_label_encoders.pkl')

# 标签编码
for col in X.columns:
    if col in label_encoders:
        le = label_encoders[col]
        # 注意：可能遇到训练时未见过的新类别，这里要做异常处理
        X[col] = X[col].map(lambda x: x if x in le.classes_ else le.classes_[0])
        # 如果有未知值，可替换为默认类或抛出警告
        X[col] = le.transform(X[col])

# 加载标准化器
scaler = joblib.load('../encoder/lo_scaler.pkl')
# 标准化新数据 注意：这里只用 transform，不要 fit_transform
X = pd.DataFrame(scaler.transform(X), columns=X.columns)
# 加载模型
lo = joblib.load('../model/lo_best.pkl')
# 预测
y_pred = lo.predict(X)

# 评估模型
print("\n=== 最终模型评估 ===")
y_pred_proba = lo.predict_proba(X)[:, 1]
print(f"AUC: {roc_auc_score(y, y_pred_proba):.4f}")
print(f"F1-score: {f1_score(y, y_pred):.4f}")
print("\n分类报告:")
print(classification_report(y, y_pred))